/// Bar chart example
import 'package:charts_flutter/flutter.dart' as charts;
import 'package:flutter/material.dart';

class SimpleBarChart extends StatelessWidget {
  List<charts.Series> seriesList;
  final bool animate;
  List<OrdinalSales> _list;
  var color;

  SimpleBarChart(this._list, {this.animate, this.color});

  @override
  Widget build(BuildContext context) {
    seriesList = _createSampleData(); //数据转换
    return new charts.BarChart(
      seriesList,
      animate: animate,
    );
  }

  /// 数据转换
  List<charts.Series<OrdinalSales, String>> _createSampleData() {
    return [
      new charts.Series<OrdinalSales, String>(
        id: 'Sales',
        colorFn: color != null
            ? (_, __) => charts.MaterialPalette.blue.shadeDefault
            : (_, __) => charts.MaterialPalette.red.shadeDefault,
        domainFn: (OrdinalSales sales, _) => sales.year,
        measureFn: (OrdinalSales sales, _) => sales.sales,
        data: _list,
      )
    ];
  }
}

///数据对象
class OrdinalSales {
  String year;
  int sales;

  OrdinalSales(this.year, this.sales);

  OrdinalSales.fromMap(Map<String, dynamic> map) {
    year = map['year'];
    sales = map['sales'];
  }
}
